<template>
  <el-card>
    <el-form inline>
      <el-form-item>
        <el-input placeholder="用户名" v-model="roleName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search"> 搜索 </el-button>
        <el-button> 清空 </el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary"> 添加用户 </el-button>
    <el-button type="danger"> 批量删除 </el-button>

    <el-table
      :data="roleList"
      v-loading="loading"
      border
      class="mt-20"
      row-key="id"
    >
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column prop="roleName" label="角色名称" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="info"
            :icon="UserFilled"
            size="small"
            @click="addRole(row)"
          />
          <el-button type="warning" :icon="Edit" size="small" />
          <el-button type="danger" :icon="Delete" size="small" />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="mt-20"
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getRoleList"
      @current-change="getRoleList"
    />

    <el-dialog v-model="dialogVisible" title="添加用户">
      <el-form
        label-width="100px"
        ref="roleFormRef"
        :model="roleFormData"
        :rules="roleFormRules"
      >
        <el-form-item label="用户名" prop="roleName">
          <el-input v-model="roleFormData.roleName" />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CRole",
};
</script>

<script lang="ts" setup>
import { Search, UserFilled, Edit, Delete } from "@element-plus/icons-vue";
import { ref, onMounted, reactive } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { getRoleListApi } from "@/api/acl/role";
import type { RoleList, RoleItem } from "@/api/acl/model/userModel";
const loading = ref();
const roleName = ref("");
const dialogVisible = ref(false);

/************************* 分页器 **************************/
const currentPage = ref(1);
const pageSize = ref(5);
const total = ref(0);
const roleList = ref<RoleList>([]);

const getRoleList = async () => {
  const res = await getRoleListApi(currentPage.value, pageSize.value);
  // console.log(res);
  roleList.value = res.items;
  total.value = res.total;
};

onMounted(getRoleList);

// 添加角色
const addRole = (row: RoleItem) => {
  roleFormData.roleName = row.roleName;
  dialogVisible.value = true;
};

/**************************** 添加角色弹框 ****************************/
const roleFormData = reactive({
  roleName: "",
});
const roleFormRef = ref<FormInstance>();
const roleFormRules = reactive<FormRules>({
  roleName: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
});
</script>

<style scoped></style>
